package com.ruoyi.project.surveys.service.impl;

import java.beans.Transient;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.surveys.mapper.SurveysQuestionMapper;
import com.ruoyi.project.surveys.domain.SurveysQuestion;
import com.ruoyi.project.surveys.service.ISurveysQuestionService;

/**
 * 题目信息Service业务层处理
 * 
 * @author zhimin-hu
 * @date 2020-05-02
 */
@Service
public class SurveysQuestionServiceImpl implements ISurveysQuestionService 
{
    @Autowired
    private SurveysQuestionMapper surveysQuestionMapper;

    /**
     * 查询题目信息
     * 
     * @param id 题目信息ID
     * @return 题目信息
     */
    @Override
    public SurveysQuestion selectSurveysQuestionById(Long id)
    {
        return surveysQuestionMapper.selectSurveysQuestionById(id);
    }

    /**
     * 查询题目信息列表
     * 
     * @param surveysQuestion 题目信息
     * @return 题目信息
     */
    @Override
    public List<SurveysQuestion> selectSurveysQuestionList(SurveysQuestion surveysQuestion)
    {
        return surveysQuestionMapper.selectSurveysQuestionList(surveysQuestion);
    }

    /**
     * 新增题目信息
     * 
     * @param surveysQuestion 题目信息
     * @return 结果
     */
    @Override
    public int insertSurveysQuestion(SurveysQuestion surveysQuestion)
    {
        surveysQuestion.setCreateTime(DateUtils.getNowDate());
        return surveysQuestionMapper.insertSurveysQuestion(surveysQuestion);
    }

    /**
     * 修改题目信息
     * 
     * @param surveysQuestion 题目信息
     * @return 结果
     */
    @Override
    @Transient
    public int updateSurveysQuestion(SurveysQuestion surveysQuestion)
    {
        surveysQuestion.setUpdateTime(DateUtils.getNowDate());
        if(surveysQuestion.getType()==0){
            //当为题目时候要联合更新子项目
            SurveysQuestion itemParem=new SurveysQuestion();
            itemParem.setSurveysNo(surveysQuestion.getSurveysNo());
            itemParem.setQuestionParentId(surveysQuestion.getQuestionNo());
            itemParem.setType(1);
            List<SurveysQuestion> item=surveysQuestionMapper.selectSurveysQuestionList(itemParem);
            if(!item.isEmpty()){
                for (SurveysQuestion question:item) {
                    question.setSurveysNo(surveysQuestion.getSurveysNo());
                    question.setOptions(surveysQuestion.getOptions());
                    question.setQuestionMotifId(surveysQuestion.getQuestionMotifId());
                    surveysQuestionMapper.updateSurveysQuestion(question);
                }
            }
        }
        return surveysQuestionMapper.updateSurveysQuestion(surveysQuestion);
    }

    /**
     * 批量删除题目信息
     * 
     * @param ids 需要删除的题目信息ID
     * @return 结果
     */
    @Override
    public int deleteSurveysQuestionByIds(Long[] ids)
    {
        return surveysQuestionMapper.deleteSurveysQuestionByIds(ids);
    }

    /**
     * 删除题目信息信息
     * 
     * @param id 题目信息ID
     * @return 结果
     */
    @Override
    public int deleteSurveysQuestionById(Long id)
    {
        return surveysQuestionMapper.deleteSurveysQuestionById(id);
    }
}
